package com.supermap.gwfs.datashare.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.supermap.gcpp.core.common.UniObject;
import com.supermap.gwfs.datashare.dao.IGridForecastDao;
import com.supermap.gwfs.datashare.entity.ForecastElement;
import com.supermap.gwfs.datashare.util.CollectionUtil;
import com.supermap.gwfs.datashare.util.Logger;
import com.supermap.gwfs.datashare.util.PageData;

/**  
 * @Description: GridForecast service
 * @author zhoujian
 * @date 2017-6-12
 * @version V1.0 
 */
@Service
public class GridForecastService
{
	@Resource
	private IGridForecastDao gridForecastDao;
	@Resource
	private ForecastElementService forecastElementService ;
	
	private Logger logger = Logger.getLogger(this.getClass());
	
	public List<Object> getGridFcstByOriginElementAndValid(PageData pd , List<UniObject> params) throws Exception
	{
		List<Object> gridForecasts = new  ArrayList<Object>();
		try
		{
			String origin = pd.getString("origin");
			//4.获取天气转换的要素的IDS
			UniObject originObject = params.get(0).getObjectsByName(origin).get(0);
			
			gridForecasts = this.getGridFcstByOriginElementAndValid(pd , originObject);
		}
		catch (Exception e)
		{
			logger.error(this.getClass().toString() + " GridForecastService exception , this exception is : " + e);
		}
		
		return gridForecasts;
	}
	/**
	 * 
	 * @Description: 根据传入的参数封装pd查询数据
	 * @return List<Object>
	 * @throws
	 */
	public List<Object> getGridFcstByOriginElementAndValid(PageData pd , UniObject originObject) throws Exception
	{
		List<Object> gridForecasts = new  ArrayList<Object>();
		//设置初始场ID
		pd.put("origin", originObject.getIntegerValue("originId"));
		//设置要素名称(单个或者多个)
		pd.put("elements", originObject.getStringValue("elements").split(","));
		//根据初始场和要素名查询该场下面的要素信息(要素ID，要素name，要素value....)
		List<ForecastElement> elements = forecastElementService.getFindAllElement(pd);
		//当传入的是具体的时效值时处理,单个时效或者多个时效
		if (pd.getString("forecast_valid") != null)
		{
			pd.put("forecast_valid", pd.getString("forecast_valid").split(","));
		}
		//获取到特殊处理的要素和版本(天气现象转换的时候会获取特殊处理的要素(特殊处理要素为环境天气要素【数组】)
		String[] special = originObject.getStringValue("special").split(",");
		//设置文件版本【中央台客观  主观    市台客观  欧洲客观】
		String version = originObject.getStringValue("version");
		//5.按初始场 日期 时次 时效 版本 查询数据库并下载文件到本地
		
		if (special.length == 0)
		{
			for (ForecastElement fe : elements)
			{
				pd.put("element", fe.getId());
				if (CollectionUtil.ArrayContain(special, fe.getVal()))//包含
				{
					pd.put("forecast_fileversion", version);
				}
				else {
					pd.put("forecast_fileversion", pd.getString("forecast_fileversion"));
				}
				List<Object> gfs = this.getGridFcstByOriginElementAndValid(pd);
				
				gridForecasts.addAll(gfs);
			}
		}
		else{
			gridForecasts = this.getGridFcstByOriginElementAndValid(pd);
		}
		
		return gridForecasts;
	}
	
	
	
	
	public List<Object> getGridFcstByOriginElementAndValid(PageData pd) throws Exception
	{
		return gridForecastDao.getGridFcstByOriginElementAndValid(pd);
	}
	
	
	public List<Object> getGridFcstByOriEleAndValVersion(PageData pd) throws Exception
	{
		return gridForecastDao.getGridFcstByOriEleAndValVersion(pd);
	}

	public List<Object> getShare(Map<String , Object> checkMap) {
		// TODO Auto-generated method stub
		return gridForecastDao.getShare(checkMap);
	}


	public List<Object> getPhaseState(Map<String, Object> checkMap) {
		// TODO Auto-generated method stub
		return gridForecastDao.getPhaseState(checkMap);
	}

}
